"""
https://lbs.amap.com/api/webservice/guide/api/georegeo

{
    "status": "1",
    "info": "OK",
    "infocode": "10000",
    "count": "1",
    "geocodes": [
        {
            "formatted_address": "湖北省武汉市江汉区黄鹤楼",
            "country": "中国",
            "province": "湖北省",
            "citycode": "027",
            "city": "武汉市",
            "district": "江汉区",
            "township": [],
            "neighborhood": {
                "name": [],
                "type": []
            },
            "building": {
                "name": [],
                "type": []
            },
            "adcode": "420103",
            "street": [],
            "number": [],
            "location": "114.267135,30.588888",
            "level": "村庄"
        }
    ]
}
"""

import json
import os

import requests

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
URL = 'https://restapi.amap.com/v3/geocode/geo'
AMAP_KEY = '5a98f7afa7a4de8a487c9323cad7a146'
SESSION = requests.Session()
SPOTS = {
    '黄鹤楼': [116.406315, 39.908775],
    '东湖落雁景区': [],
    '木兰天池': [],
    '木兰草原': [],
    '云雾山': [],
    '木兰山': [],
    '锦里沟': [],
    '清凉寨': [],
    '武汉植物园': [],
    '东湖飞鸟世界': [],
    '古琴台': [],
    '长春观': [],
    '张公山寨': [],
    '农耕年华': [],
    '黄鹤楼落梅轩': [],
    '九峰森林动物园': [],
    '木兰湖': [],
    '九真山': [],
    '龙泉山': [],
    '紫薇都市田园': [],
    '木兰玫瑰花园': [],
    '解密汉秀剧场（非演出票）': [],
    '园博园': [],
    '姚家山': [],
    '木兰三台山': [],
    '木兰花海乐园': [],
    '九真桃源': [],
    '东湖游船（改造中）': [],
    '两江游览': [],
    '小雷山': [],
    '三里畈沙雕公园': [],
    '黄石矿山公园': [],
    '薄刀峰': [],
    '凤娃古寨': [],
    '木兰花乡': [],
    '雷山温泉': [],
    '山湖温泉': [],
    '三里畈温泉': [],
    '金龙水寨': [],
    '白兆山': [],
    '双峰山': [],
    '金卉庄园': [],
    '天紫湖': [],
    '木兰水镇': [],
    '木兰花谷': [],
}

ALIASES = {
    '张公山寨': '武汉市青山区张公山寨景区',
    '解密汉秀剧场（非演出票）': '汉秀剧场',
    '姚家山': '',
    '九真桃源': '湖北省武汉市蔡甸区九真桃源景区',
    '小雷山': '',
    '三里畈沙雕公园': '',
    '黄石矿山公园': '',
    '薄刀峰': '',
    '雷山温泉': '',
    '山湖温泉': '',
    '三里畈温泉': '',
    '白兆山': '',
    '双峰山': '湖北省孝感市双峰山旅游度假区',
    '金卉庄园': '',
}
SPOTS_CITY = {
    '张公山寨': '武汉',
    '解密汉秀剧场（非演出票）': '武汉',
    '姚家山': '',  # 黄陂区蔡店街道
    '九真桃源': '',  # 蔡甸区永安街，地处九真山国家森林公园
    '小雷山': '黄石',  # 大冶
    '三里畈沙雕公园': '黄冈',  # 罗田
    '黄石矿山公园': '黄石',  # 黄石市铁山区
    '薄刀峰': '黄冈',  # 罗田
    '雷山温泉': '',  # 黄石 > 大冶
    '山湖温泉': '咸宁',  # 嘉鱼
    '三里畈温泉': '黄冈',  # 罗田
    '白兆山': '安陆',
    '双峰山': '孝感',  # 孝感
    '金卉庄园': '孝感',
}


def main():
    json_path = os.path.join(CURRENT_DIR, 'whlynk.json')
    if os.path.exists(json_path):
        with open(json_path) as fp:
            data = json.load(fp)
    else:
        data = {}

    for address in SPOTS:
        if data.get(address):
            continue

        print('get %s' % address)
        # continue

        address_name = ALIASES.get(address, address) or address
        resp = SESSION.get(URL, params={
            'key': AMAP_KEY,
            'address': address_name,
            'city': SPOTS_CITY.get(address, '武汉'),
        })
        addr_data = resp.json()
        print(json.dumps(addr_data, indent=4, ensure_ascii=False))
        if addr_data['status'] != '1':
            print('status error: %s' % address_name)
            data[address] = {}
        elif not addr_data['geocodes']:
            print('empty geocodes: %s' % address_name)
            data[address] = {}
        elif addr_data['count'] != '1':
            print('many geocodes: %s' % address_name)
            data[address] = {}
        else:
            data[address] = addr_data['geocodes'][0]

    with open(os.path.join(CURRENT_DIR, 'whlynk.json'), 'w') as fp:
        json.dump(data, fp, indent=4, ensure_ascii=False)


if __name__ == '__main__':
    main()
